{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib\n",
    "import os\n",
    "from keras import models \n",
    "from keras import layers \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_01 = joblib.load('../../3.HA_model/1.PCA_T2/train_HA_feature/train_01.lz4')\n",
    "train_02 = joblib.load('../../3.HA_model/1.PCA_T2/train_HA_feature/train_02.lz4')\n",
    "train_03 = joblib.load('../../3.HA_model/1.PCA_T2/train_HA_feature/train_03.lz4')\n",
    "\n",
    "test_01 = joblib.load('../../3.HA_model/1.PCA_T2/test_HA_feature/test_01.lz4')\n",
    "test_02 = joblib.load('../../3.HA_model/1.PCA_T2/test_HA_feature/test_02.lz4')\n",
    "test_03 = joblib.load('../../3.HA_model/1.PCA_T2/test_HA_feature/test_03.lz4')\n",
    "test_04 = joblib.load('../../3.HA_model/1.PCA_T2/test_HA_feature/test_04.lz4')\n",
    "test_05 = joblib.load('../../3.HA_model/1.PCA_T2/test_HA_feature/test_05.lz4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.concat([train_01, train_02, train_03], axis=0)\n",
    "y_train = train[['RULR']]\n",
    "use_cols = ['PCA_T2__mean', 'PCA_T2__mean_diff', 'PCA_T2__delta', 'PCA_T2__delta_diff']\n",
    "x_train = train[use_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = pd.concat([test_01, test_02, test_03, test_04, test_05], axis=0)\n",
    "use_cols = ['PCA_T2__mean', 'PCA_T2__mean_diff', 'PCA_T2__delta', 'PCA_T2__delta_diff']\n",
    "x_test = test[use_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model(): \n",
    "    model = models.Sequential() \n",
    "    model.add(layers.Dense(50,activation='relu',input_shape=(4,))) \n",
    "    model.add(layers.Dense(20,activation='relu')) \n",
    "    model.add(layers.Dense(10,activation='relu')) \n",
    "    model.add(layers.Dense(1)) \n",
    "    model.compile(optimizer='sgd', \n",
    "                  loss='mean_squared_error', # 等价于loss = losses.binary_crossentropy \n",
    "                  metrics=['mae'] # 等价于metrics = [metircs.binary_accuracy]\n",
    "                  )  \n",
    "    return model \n",
    "model = build_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.36113585, 6.39620562, 7.9223947 , 1.0442634 ])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train = x_train.values*np.array([1,100,10,100])\n",
    "np.mean(x_train, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "133/133 [==============================] - 0s 64us/step - loss: 2.1628 - mean_absolute_error: 0.9864\n",
      "Epoch 2/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 5.0794 - mean_absolute_error: 1.3720\n",
      "Epoch 3/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0161 - mean_absolute_error: 0.0921\n",
      "Epoch 4/100\n",
      "133/133 [==============================] - 0s 53us/step - loss: 0.0136 - mean_absolute_error: 0.0801\n",
      "Epoch 5/100\n",
      "133/133 [==============================] - 0s 48us/step - loss: 0.0124 - mean_absolute_error: 0.0772\n",
      "Epoch 6/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0112 - mean_absolute_error: 0.0741\n",
      "Epoch 7/100\n",
      "133/133 [==============================] - 0s 50us/step - loss: 0.0104 - mean_absolute_error: 0.0733\n",
      "Epoch 8/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0093 - mean_absolute_error: 0.0685\n",
      "Epoch 9/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0086 - mean_absolute_error: 0.0661\n",
      "Epoch 10/100\n",
      "133/133 [==============================] - 0s 42us/step - loss: 0.0078 - mean_absolute_error: 0.0628\n",
      "Epoch 11/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0072 - mean_absolute_error: 0.0607\n",
      "Epoch 12/100\n",
      "133/133 [==============================] - 0s 42us/step - loss: 0.0068 - mean_absolute_error: 0.0594\n",
      "Epoch 13/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0064 - mean_absolute_error: 0.0594\n",
      "Epoch 14/100\n",
      "133/133 [==============================] - 0s 42us/step - loss: 0.0061 - mean_absolute_error: 0.0586\n",
      "Epoch 15/100\n",
      "133/133 [==============================] - 0s 41us/step - loss: 0.0058 - mean_absolute_error: 0.0569\n",
      "Epoch 16/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0054 - mean_absolute_error: 0.0548\n",
      "Epoch 17/100\n",
      "133/133 [==============================] - 0s 42us/step - loss: 0.0052 - mean_absolute_error: 0.0539\n",
      "Epoch 18/100\n",
      "133/133 [==============================] - 0s 41us/step - loss: 0.0049 - mean_absolute_error: 0.0532\n",
      "Epoch 19/100\n",
      "133/133 [==============================] - 0s 44us/step - loss: 0.0047 - mean_absolute_error: 0.0512\n",
      "Epoch 20/100\n",
      "133/133 [==============================] - 0s 61us/step - loss: 0.0045 - mean_absolute_error: 0.0505\n",
      "Epoch 21/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0043 - mean_absolute_error: 0.0494\n",
      "Epoch 22/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0042 - mean_absolute_error: 0.0500\n",
      "Epoch 23/100\n",
      "133/133 [==============================] - 0s 45us/step - loss: 0.0041 - mean_absolute_error: 0.0506\n",
      "Epoch 24/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0038 - mean_absolute_error: 0.0481\n",
      "Epoch 25/100\n",
      "133/133 [==============================] - 0s 40us/step - loss: 0.0037 - mean_absolute_error: 0.0472\n",
      "Epoch 26/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0036 - mean_absolute_error: 0.0471\n",
      "Epoch 27/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0035 - mean_absolute_error: 0.0467\n",
      "Epoch 28/100\n",
      "133/133 [==============================] - 0s 41us/step - loss: 0.0034 - mean_absolute_error: 0.0470\n",
      "Epoch 29/100\n",
      "133/133 [==============================] - 0s 40us/step - loss: 0.0032 - mean_absolute_error: 0.0447\n",
      "Epoch 30/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0032 - mean_absolute_error: 0.0453\n",
      "Epoch 31/100\n",
      "133/133 [==============================] - 0s 68us/step - loss: 0.0031 - mean_absolute_error: 0.0434\n",
      "Epoch 32/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0030 - mean_absolute_error: 0.0430\n",
      "Epoch 33/100\n",
      "133/133 [==============================] - 0s 41us/step - loss: 0.0030 - mean_absolute_error: 0.0428\n",
      "Epoch 34/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0029 - mean_absolute_error: 0.0428\n",
      "Epoch 35/100\n",
      "133/133 [==============================] - 0s 60us/step - loss: 0.0029 - mean_absolute_error: 0.0417\n",
      "Epoch 36/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0028 - mean_absolute_error: 0.0413\n",
      "Epoch 37/100\n",
      "133/133 [==============================] - 0s 42us/step - loss: 0.0027 - mean_absolute_error: 0.0418\n",
      "Epoch 38/100\n",
      "133/133 [==============================] - 0s 45us/step - loss: 0.0027 - mean_absolute_error: 0.0403\n",
      "Epoch 39/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0026 - mean_absolute_error: 0.0401\n",
      "Epoch 40/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0026 - mean_absolute_error: 0.0403\n",
      "Epoch 41/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0026 - mean_absolute_error: 0.0396\n",
      "Epoch 42/100\n",
      "133/133 [==============================] - 0s 45us/step - loss: 0.0026 - mean_absolute_error: 0.0403\n",
      "Epoch 43/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0025 - mean_absolute_error: 0.0393\n",
      "Epoch 44/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0025 - mean_absolute_error: 0.0393\n",
      "Epoch 45/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0025 - mean_absolute_error: 0.0408\n",
      "Epoch 46/100\n",
      "133/133 [==============================] - 0s 50us/step - loss: 0.0024 - mean_absolute_error: 0.0387\n",
      "Epoch 47/100\n",
      "133/133 [==============================] - 0s 55us/step - loss: 0.0024 - mean_absolute_error: 0.0393\n",
      "Epoch 48/100\n",
      "133/133 [==============================] - 0s 56us/step - loss: 0.0024 - mean_absolute_error: 0.0391\n",
      "Epoch 49/100\n",
      "133/133 [==============================] - 0s 50us/step - loss: 0.0023 - mean_absolute_error: 0.0390\n",
      "Epoch 50/100\n",
      "133/133 [==============================] - 0s 55us/step - loss: 0.0023 - mean_absolute_error: 0.0379\n",
      "Epoch 51/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0023 - mean_absolute_error: 0.0381\n",
      "Epoch 52/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0023 - mean_absolute_error: 0.0371\n",
      "Epoch 53/100\n",
      "133/133 [==============================] - 0s 53us/step - loss: 0.0022 - mean_absolute_error: 0.0385\n",
      "Epoch 54/100\n",
      "133/133 [==============================] - 0s 52us/step - loss: 0.0022 - mean_absolute_error: 0.0377\n",
      "Epoch 55/100\n",
      "133/133 [==============================] - 0s 53us/step - loss: 0.0022 - mean_absolute_error: 0.0371\n",
      "Epoch 56/100\n",
      "133/133 [==============================] - 0s 51us/step - loss: 0.0022 - mean_absolute_error: 0.0375\n",
      "Epoch 57/100\n",
      "133/133 [==============================] - 0s 51us/step - loss: 0.0022 - mean_absolute_error: 0.0382\n",
      "Epoch 58/100\n",
      "133/133 [==============================] - 0s 44us/step - loss: 0.0021 - mean_absolute_error: 0.0380\n",
      "Epoch 59/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0021 - mean_absolute_error: 0.0385\n",
      "Epoch 60/100\n",
      "133/133 [==============================] - 0s 52us/step - loss: 0.0021 - mean_absolute_error: 0.0371\n",
      "Epoch 61/100\n",
      "133/133 [==============================] - 0s 51us/step - loss: 0.0021 - mean_absolute_error: 0.0362\n",
      "Epoch 62/100\n",
      "133/133 [==============================] - 0s 52us/step - loss: 0.0020 - mean_absolute_error: 0.0364\n",
      "Epoch 63/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0020 - mean_absolute_error: 0.0359\n",
      "Epoch 64/100\n",
      "133/133 [==============================] - 0s 52us/step - loss: 0.0020 - mean_absolute_error: 0.0359\n",
      "Epoch 65/100\n",
      "133/133 [==============================] - 0s 50us/step - loss: 0.0020 - mean_absolute_error: 0.0352\n",
      "Epoch 66/100\n",
      "133/133 [==============================] - 0s 48us/step - loss: 0.0020 - mean_absolute_error: 0.0367\n",
      "Epoch 67/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0020 - mean_absolute_error: 0.0351\n",
      "Epoch 68/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0019 - mean_absolute_error: 0.0349\n",
      "Epoch 69/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0019 - mean_absolute_error: 0.0357\n",
      "Epoch 70/100\n",
      "133/133 [==============================] - 0s 51us/step - loss: 0.0019 - mean_absolute_error: 0.0346\n",
      "Epoch 71/100\n",
      "133/133 [==============================] - 0s 62us/step - loss: 0.0020 - mean_absolute_error: 0.0359\n",
      "Epoch 72/100\n",
      "133/133 [==============================] - 0s 54us/step - loss: 0.0019 - mean_absolute_error: 0.0346\n",
      "Epoch 73/100\n",
      "133/133 [==============================] - 0s 51us/step - loss: 0.0019 - mean_absolute_error: 0.0357\n",
      "Epoch 74/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0019 - mean_absolute_error: 0.0346\n",
      "Epoch 75/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0019 - mean_absolute_error: 0.0349\n",
      "Epoch 76/100\n",
      "133/133 [==============================] - 0s 50us/step - loss: 0.0019 - mean_absolute_error: 0.0362\n",
      "Epoch 77/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0019 - mean_absolute_error: 0.0349\n",
      "Epoch 78/100\n",
      "133/133 [==============================] - 0s 43us/step - loss: 0.0018 - mean_absolute_error: 0.0341\n",
      "Epoch 79/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0019 - mean_absolute_error: 0.0343\n",
      "Epoch 80/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0018 - mean_absolute_error: 0.0335\n",
      "Epoch 81/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0018 - mean_absolute_error: 0.0337\n",
      "Epoch 82/100\n",
      "133/133 [==============================] - 0s 44us/step - loss: 0.0018 - mean_absolute_error: 0.0339\n",
      "Epoch 83/100\n",
      "133/133 [==============================] - 0s 73us/step - loss: 0.0018 - mean_absolute_error: 0.0343\n",
      "Epoch 84/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0018 - mean_absolute_error: 0.0338\n",
      "Epoch 85/100\n",
      "133/133 [==============================] - 0s 56us/step - loss: 0.0018 - mean_absolute_error: 0.0339\n",
      "Epoch 86/100\n",
      "133/133 [==============================] - 0s 59us/step - loss: 0.0018 - mean_absolute_error: 0.0334\n",
      "Epoch 87/100\n",
      "133/133 [==============================] - 0s 52us/step - loss: 0.0018 - mean_absolute_error: 0.0332\n",
      "Epoch 88/100\n",
      "133/133 [==============================] - 0s 55us/step - loss: 0.0018 - mean_absolute_error: 0.0332\n",
      "Epoch 89/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0018 - mean_absolute_error: 0.0339\n",
      "Epoch 90/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0018 - mean_absolute_error: 0.0329\n",
      "Epoch 91/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0017 - mean_absolute_error: 0.0325\n",
      "Epoch 92/100\n",
      "133/133 [==============================] - 0s 45us/step - loss: 0.0017 - mean_absolute_error: 0.0330\n",
      "Epoch 93/100\n",
      "133/133 [==============================] - 0s 48us/step - loss: 0.0017 - mean_absolute_error: 0.0333\n",
      "Epoch 94/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0017 - mean_absolute_error: 0.0334\n",
      "Epoch 95/100\n",
      "133/133 [==============================] - 0s 46us/step - loss: 0.0017 - mean_absolute_error: 0.0331\n",
      "Epoch 96/100\n",
      "133/133 [==============================] - 0s 45us/step - loss: 0.0017 - mean_absolute_error: 0.0327\n",
      "Epoch 97/100\n",
      "133/133 [==============================] - 0s 44us/step - loss: 0.0017 - mean_absolute_error: 0.0320\n",
      "Epoch 98/100\n",
      "133/133 [==============================] - 0s 47us/step - loss: 0.0017 - mean_absolute_error: 0.0328\n",
      "Epoch 99/100\n",
      "133/133 [==============================] - 0s 49us/step - loss: 0.0017 - mean_absolute_error: 0.0341\n",
      "Epoch 100/100\n",
      "133/133 [==============================] - 0s 53us/step - loss: 0.0017 - mean_absolute_error: 0.0327\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f86c532d278>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train, y_train, epochs=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.47193077, 0.4723193 , 0.47264838, 0.4729292 , 0.4731658 ,\n",
       "        0.47336406, 0.47352862, 0.47366318, 0.47267252, 0.47142226],\n",
       "       [0.52137303, 0.4950058 , 0.46521372, 0.4317912 , 0.39453325,\n",
       "        0.35323492, 0.30769175, 0.25770012, 0.20305577, 0.14355582],\n",
       "       [0.51791966, 0.51321024, 0.5087241 , 0.5044339 , 0.5003175 ,\n",
       "        0.49635696, 0.4925369 , 0.48884445, 0.48526847, 0.4817993 ],\n",
       "       [0.67313576, 0.6445712 , 0.61340654, 0.5790217 , 0.5359909 ,\n",
       "        0.5047958 , 0.48562706, 0.46487528, 0.44248572, 0.41840434],\n",
       "       [0.42071533, 0.3972732 , 0.3724    , 0.3460645 , 0.31823587,\n",
       "        0.2889061 , 0.25814748, 0.22580588, 0.19185176, 0.15623423]],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(x_test).reshape(-1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
